﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class DocumenteSearchFrm : FrmRezumat
    {

        public String Filtru = String.Empty;
        public Boolean bCauta = true;
        public Boolean parteneriProprii = false;
        public Boolean perioadaCurenta = false;
        private int lunaCurenta = 0;
        private int anulCurent = 0;
        private Boolean DocumenteIntrare = false;

        public DocumenteSearchFrm(ModuleClass mc,Boolean _partprop,Boolean _perCrt,int _lunaCurenta,int _anulCurent,Boolean _docIntr) :base(mc)
        {
            InitializeComponent();
            workConnString = mc.workConnectionString;
            ActiveControl = numarulTB;
            parteneriProprii = _partprop;
            EnableMainBrowseButton = false;
            perioadaCurenta = _perCrt;
            lunaCurenta = _lunaCurenta;
            anulCurent = _anulCurent;
            DocumenteIntrare = _docIntr;
            documenteTableAdapter.SetTableName(mc.AnArhiva);
        }

        private void DocumenteSearchFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.articoleTableAdapter.Connection = con;
            this.articoleTableAdapter.Fill(this.eSRPDataSet.Articole, ModuleInfo.CodUnitate);
            fares_ParteneriCUITableAdapter.Connection = con;
            if(!parteneriProprii)
                this.fares_ParteneriCUITableAdapter.Fill(this.eSRPDataSet.fares_ParteneriCUI);
            else
                this.fares_ParteneriCUITableAdapter.FillByUnitate(this.eSRPDataSet.fares_ParteneriCUI, ModuleInfo.CodUnitate);
            InchideConexiune(con);
            AddButton("Filtreaza[F9]", global::ERPStudio.Properties.Resources._001_371, Cauta, ToolStripItemAlignment.Right, false);
            //AddButton("Filtreaza", global::ERPStudio.Properties.Resources._001_38, Filtreaza, ToolStripItemAlignment.Right, false);
            HandleSalvare += Cauta;
        }

        private void addPartenerB_Click(object sender, EventArgs e)
        {
            furnizoriArticoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(partenerCB.Text,
                Convert.ToInt64(partenerCB.SelectedValue)));
            parteneriRB.Checked = true;
        }

        private void delPart_Click(object sender, EventArgs e)
        {
            if (furnizoriArticoleSelectateBindingSource.Count > 0)
                furnizoriArticoleSelectateBindingSource.RemoveCurrent();
        }

        private void addArt_Click(object sender, EventArgs e)
        {
            furnizoriArticoleSelectateBindingSource1.Add(new FurnizoriArticoleSelectate(articolCB.Text,
                Convert.ToInt16(articolCB.SelectedValue)));
            articoleRB.Checked = true;
        }

        private void delArt_Click(object sender, EventArgs e)
        {
            if (furnizoriArticoleSelectateBindingSource1.Count > 0)
                furnizoriArticoleSelectateBindingSource1.RemoveCurrent();
        }


        private void deLaDataDTP_Enter(object sender, EventArgs e)
        {
            dateRB.Checked = true;
        }

        private void laDataDTP_Enter(object sender, EventArgs e)
        {
            dataRB.Checked = true;
        }

        private void partenerCB_Enter(object sender, EventArgs e)
        {
            partenerRB.Checked = true;
        }

        private void articolCB_Enter(object sender, EventArgs e)
        {
            articolRB.Checked = true;
        }

        

        new private void Cauta(object sender, EventArgs e)
        {
            Filtru = String.Empty;
            if(parteneriProprii)
                Filtru = " [Documente"+ModuleInfo.AnArhiva+"].CodUnitate=" + ModuleInfo.CodUnitate.ToString();

            if (Filtru != String.Empty)
                Filtru += " AND ";
            Filtru += " (IDTipDocument IN (SELECT IDTipDocument FROM TipDocument WHERE (TipOperatie LIKE '%" + (DocumenteIntrare?"I":"O") + "%')))";              

            
            if (perioadaCurenta)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " MONTH([DataDoc]) >= " + lunaCurenta.ToString() + " AND YEAR([DataDoc])>=" + anulCurent.ToString();              
            }
            if (dataRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "DataDoc='" + laDataDTP.Value.Year.ToString() + "-" + laDataDTP.Value.Month.ToString() + "-" +
                    laDataDTP.Value.Day.ToString() + "'";
            }
            if (dateRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";

                Filtru = "DataDoc>='" + deLaDataDTP.Value.Year.ToString() + "-" + deLaDataDTP.Value.Month.ToString() + "-" +
                    deLaDataDTP.Value.Day.ToString() + "' and DataDoc<='" + panaLaDataDTP.Value.Year.ToString() + "-" + panaLaDataDTP.Value.Month.ToString() + "-" +
                    panaLaDataDTP.Value.Day.ToString() + "'";
            }
            if (partenerRB.Checked && partenerCB.SelectedValue!=null)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDPartener=" + partenerCB.SelectedValue.ToString();
            }
            if (parteneriRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDPartener in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource.List[i]).Tag.ToString();
                }
                Filtru += filt + ")";
            }
            if (articolRB.Checked && articolCB.SelectedValue!=null)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDArticol=" + articolCB.SelectedValue.ToString();
            
            }
            if (numarulTB.Text != String.Empty)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " NrDoc like '" + numarulTB.Text +"'";
            }
            if (articoleRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDArticol in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource1.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource1.List[i]).Tag.ToString();
                }
                Filtru += filt + ")";

            }

            documenteTableAdapter.Connection = DeschideConexiune(workConnString);
            documenteTableAdapter.FillByFiltru(eSRPDataSet.Documente, Filtru,ModuleInfo.AnArhiva);
            InchideConexiune(documenteTableAdapter.Connection);
            if (documenteBS.Count == 0)
            {
                MessageBox.Show("Nu s-a gasit nici o inregistrare care sa satisfaca criteriile selectate!",
                       "Atentie",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Hand);
                return;
            }
            this.DialogResult = DialogResult.OK;
            Close();

        }

        private void Filtreaza(object sender, EventArgs e)
        {
            bCauta = false;
            Cauta(sender,e);
        }

        private void dateRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dateRB.Checked)
                dataRB.Checked = false;
        }

        private void dataRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dataRB.Checked)
                dateRB.Checked = false;
        }

        private void partenerRB_CheckedChanged(object sender, EventArgs e)
        {
            if (partenerRB.Checked)
                parteneriRB.Checked = false;

        }

        private void parteneriRB_CheckedChanged(object sender, EventArgs e)
        {
            if (parteneriRB.Checked)
                partenerRB.Checked = false;

        }

        private void articolRB_CheckedChanged(object sender, EventArgs e)
        {
            if (articolRB.Checked)
                articoleRB.Checked = false;

        }

        private void articoleRB_CheckedChanged(object sender, EventArgs e)
        {
            if (articoleRB.Checked)
                articolRB.Checked = false;
        }


    }

    [Serializable]
    public class VarianteArticoleSelectat
    {
        public VarianteArticoleSelectat(String nume, Int32 tag, Decimal pv)
        {
            _Name = nume;
            _Tag = tag;
            _PretVanzare = pv;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Int32 _Tag = 0;
        [System.ComponentModel.Bindable(true)]
        public Int32 Tag
        {
            get
            {
                return _Tag;
            }
            set
            {
                _Tag = value;
            }
        }
        private Decimal _PretVanzare = Decimal.Zero;

        public Decimal PretVanzare
        {
            get { return _PretVanzare; }
            set { _PretVanzare = value; }
        }
    }


    [Serializable]
    public class FurnizoriArticoleSelectate
    {
        public FurnizoriArticoleSelectate(String nume, Decimal tag)
        {
            _Name = nume;
            _Tag2 = tag;
        }

        public FurnizoriArticoleSelectate(String nume, Int64 tag)
        {
            _Name = nume;
            _Tag = tag;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Int64 _Tag = 0;
        [System.ComponentModel.Bindable(true)]
        public Int64 Tag
        {
            get
            {
                return _Tag;
            }
            set
            {
                _Tag = value;
            }
        }

        private Decimal _Tag2 = 0;
        [System.ComponentModel.Bindable(true)]
        public Decimal Tag2
        {
            get
            {
                return _Tag2;
            }
            set
            {
                _Tag2 = value;
            }
        }
    }
}
